package com.nit.lab.mapper.medical;

import com.nit.lab.dto.medical.InfoDTO;
import com.nit.lab.dto.medical.SessionDTO;
import com.nit.lab.entity.medical.SessionEntity;
import org.apache.ibatis.annotations.*;

import java.util.List;

public interface SessionMapper {

    @Select({"<script>SELECT " +
            "ecg_session.id AS `key`, ecg_session.id, id_device, id_date, id_time, ecg_username, login_ip, description_cn, id_end_date, id_end_time " +
            "FROM ecg_session, ecg_end_session_reason where true " +
            "AND id_end_reason = ecg_end_session_reason.id " +
            "AND id_device = #{id_device} " +
            "<if test='ecg_username != null'> AND ecg_username LIKE CONCAT ('%', #{ecg_username}, '%') </if> " +
            "ORDER BY " +
            "<if test='sortField != null'> " +
            "${sortField} ${sortOrder}, " +
            "</if> " +
            "ecg_session.id ASC " +
            "LIMIT #{start}, #{length} " +
            "</script>"})
    List<SessionDTO> list4Table(@Param("start") Integer start,
                                @Param("length") Integer length,
                                @Param("ecg_username") String ecgUsername,
                                @Param("id_device") String idDevice,
                                @Param("sortField") String sortField,
                                @Param("sortOrder") String sortOrder) throws Exception;

    @Select({"<script>SELECT COUNT(*) " +
            "FROM ecg_session where true" +
            "<if test='ecg_username != null'> AND ecg_username LIKE CONCAT ('%', #{ecg_username}, '%') </if> " +
            "<if test='id_device != null'> AND id_device LIKE CONCAT ('%', #{id_device}, '%') </if> " +
            "</script>"})
    Long count4Table(@Param("ecg_username") String ecgUsername,@Param("id_device") String idDevice )throws Exception;

    @Insert("INSERT INTO ecg_session (" +
            "id_device ,id_date, id_time, ecg_username, login_ip, id_end_reason, id_end_date, id_end_time ) " +
            "VALUES (#{entity.idDevice}, #{entity.idDate}, #{entity.idTime}, #{entity.ecgUsername}, #{entity.loginIp}, #{entity.idEndReason}, #{entity.idEndDate}, #{entity.idEndTime} " +
            ")")
    @Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
    Integer add(@Param("entity") SessionEntity sessionEntity) throws Exception;

    @Update("UPDATE ecg_session " +
            "SET id_device = #{entity.idDevice},  " +
            "id_date = #{entity.idDate}, " +
            "id_time = #{entity.idTime}, " +
            "ecg_username = #{entity.ecgUsername} " +
            "login_ip = #{entity.loginIp} " +
            "id_end_reason = #{entity.idEndReason} " +
            "id_end_date = #{entity.idEndDate} " +
            "id_end_time = #{entity.idEndTime} " +
            "WHERE id = #{entity.id}")
    Integer update(@Param("entity") SessionEntity sessionEntity) throws Exception;

    @Delete("DELETE FROM ecg_session WHERE id = #{id}")
    Integer remove(@Param("id") int id) throws Exception;

    @Select("SELECT dt_time, ecg_samples, id_session " +
            "FROM ecg_ecg_data " +
            "WHERE TRUE and id_session = #{id} "
    )
    List<InfoDTO> getInfo(@Param("id") int id) throws Exception;
}
